<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>

<%
	String name = String.valueOf(session.getAttribute("session_name"));
	
	if(session.getAttribute("session_name") == null){
		String destination ="login.jsp";
		response.sendRedirect(response.encodeRedirectURL(destination));
	}else{
		boolean admin = false;
		admin = (Boolean)session.getAttribute("is_admin");
		
		if (admin == false){
			String destination ="index.jsp";
			response.sendRedirect(response.encodeRedirectURL(destination));
		}
	}
%>

<sql:query var="country" dataSource="jdbc/lut2">
   SELECT full_name FROM country
</sql:query>
<sql:query var="users" dataSource="jdbc/lut2">
   SELECT uname FROM admin_users
</sql:query>
<sql:query var="common_users" dataSource="jdbc/lut2">
   SELECT uname FROM users
</sql:query>
<sql:query var="schools" dataSource="jdbc/lut2">
   SELECT full_name, short_name FROM school
</sql:query>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <link rel="stylesheet" type="text/css" href="lutstyle.css" />
      <title>LUTAdmin</title>
      
      <script>
      	function removeCountry(country){
      		alert(country);
      		
      		
      	}
      	
      	function removeUser(user){
      		alert(user);
      		
      		
      	}
      	
      	function removeSchool(school){
      		alert(school);
      		
      		
      	}
      </script>
      
   </head>
   <body>
   
   <a href="login.jsp">Login</a> &#187; <a href="index.jsp">Main</a> &#187; LUT Administration page
   <br></br>
      <table border="1">
         <thead>
            <tr>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>
                  <h1>Manage countries</h1>
                  <table class="admin_table">
                     <thead>
                        <tr>
                           <th>List over existing in the system countries:</th>
                        </tr>
                     </thead>
                     <tbody>
                        <tr>
                           <td>
                            <div class="admin_table_div">
                              <c:forEach var="row" items="${country.rowsByIndex}">
                                 <c:forEach var="column" items="${row}">
                                    <c:out value="${column}" />
                                    <img src="img/Delete.png" class="admin_delete" alt="remove country" height="16" width="16" align="right" onmouseover="this.src='img/Delete_red.png';" onmouseout="this.src='img/Delete.png';" onclick="JavaScript:window.location='removecountry.jsp?country=<c:out value="${column}" />';" /><br />
                                 </c:forEach>
                              </c:forEach>
                              </div>
                           </td>
                        </tr>
                     </tbody>
                  </table>
                                    <form action="addcountry.jsp">
                     <table>
<tr>
<th>
Add new: 
</th><th>Short Name</th><th>Full Name</th>
</tr>
<tr>
<td></td> <td><input type="text" name="short_name" placeholder="..." onfocus="if(this.placeholder == '...') { this.placeholder=''}" onblur="if(this.placeholder == '') { this.placeholder='...'}" maxlength="7" size="7"/> </td><td><input type="text" name="fullname" placeholder="..." onfocus="if(this.placeholder == '...') { this.placeholder=''}" onblur="if(this.placeholder == '') { this.placeholder='...'}" /></td>
</tr>
<tr>
<td>
<input type="submit" value="Add" />
</td>
</tr>
</table>
</form>
               </td>
               <td>
                  <h1>Manage schools</h1>
                  <table class="admin_table">
                     <thead>
                        <tr>
                           <th>List over existing in the system schools:</th>
                        </tr>
                     </thead>
                     <tbody>
                        <tr>
                           <td>
                           <div class="admin_table_div">
                              <c:forEach var="row" items="${schools.rowsByIndex}">
                                 <c:out value="${row[0]}" />
                                 (
                                 <c:out value="${row[1]}" />
                                 ) <img src="img/Delete.png" class="admin_delete" alt="remove country" height="16" width="16" align="right" onmouseover="this.src='img/Delete_red.png';" onmouseout="this.src='img/Delete.png';" onclick="window.location='removeschool.jsp?schoolname=<c:out value="${row[0]}" />'; " /><br />
                              </c:forEach>
                              					</div>
                           </td>
                        </tr>
                     </tbody>
                  </table>
                  <form action="addschool.jsp">
                     <table>
<tr>
<th>
Add new: 
</th><th>Short Name</th><th>Full Name</th><th>City</th><th>Zip</th><th>Country</th>
</tr>
<tr>
<td></td> <td><input type="text" name="full_name" placeholder="..." onfocus="if(this.placeholder == '...') { this.placeholder=''}" onblur="if(this.placeholder == '') { this.placeholder='...'}" maxlength="7" size="7"/> </td><td><input type="text" name="shortname" placeholder="..." onfocus="if(this.placeholder == '...') { this.placeholder=''}" onblur="if(this.placeholder == '') { this.placeholder='...'}" /></td><td><input type="text" name="place" placeholder="..." onfocus="if(this.placeholder == '...') { this.placeholder=''}" onblur="if(this.placeholder == '') { this.placeholder='...'}" maxlength="7" size="7"/> </td><td><input type="text" name="zip" placeholder="..." onfocus="if(this.placeholder == '...') { this.placeholder=''}" onblur="if(this.placeholder == '') { this.placeholder='...'}" maxlength="4" size="4"/> </td><td><input type="text" name="country" placeholder="..." onfocus="if(this.placeholder == '...') { this.placeholder=''}" onblur="if(this.placeholder == '') { this.placeholder='...'}" maxlength="4" size="4"/> </td>
</tr>
<tr>
<td>
<input type="submit" value="Add" />
</td>
</tr>
</table>
                  </form>
               </td>
            </tr>
            <tr>
               <td>
                  <h1>Manage users</h1>
                  <table class="admin_table2">
                     <thead>
                        <tr>
                           <!-- <th>List over existing in the system users:</th> -->
                           <td><u>Admin users:</u></td>
                           <td><u>Common users:</u></td>
                        </tr>
                     </thead>
                     <tbody>
                        <tr>
                           <td>
                              <c:forEach var="row" items="${users.rowsByIndex}">
                                 <c:forEach var="column" items="${row}">
                                    <c:out value="${column}" />
                                    <img src="img/Delete.png" class="admin_delete" alt="remove country" height="16" width="16" align="right" onmouseover="this.src='img/Delete_red.png';" onmouseout="this.src='img/Delete.png';" onclick="JavaScript:window.location='removeuser.jsp?username=<c:out value="${column}" />';" /><br />
                                 </c:forEach>
                              </c:forEach>
                           </td>
                                                      <form action="promote_user.jsp">
                           <td>
                              <c:forEach var="row" items="${common_users.rowsByIndex}">
                                 <c:forEach var="column" items="${row}">
                                 <input type="radio" name="promote" value="${column}">&nbsp;
                                    <c:out value="${column}" />
                                    <img src="img/Delete.png" class="admin_delete" alt="remove country" height="16" width="16" align="right" onmouseover="this.src='img/Delete_red.png';" onmouseout="this.src='img/Delete.png';" onclick="JavaScript:window.location='removecommonuser.jsp?username=<c:out value="${column}" />';" /> <br />
                                 </c:forEach>
                              </c:forEach>
                              
                                <input type="submit" value="Make selected user admin" class="promote_button"/>
                           </td>
                              </form>
                        </tr>
                     </tbody>
                  </table>
               </td>
            </tr>
         </tbody>
      </table>
      
              <form action="logout.jsp" >
<input type="submit" value="Logout">
</form>

   </body>
</html>